﻿using System.Collections.Generic;
using System.Text;

namespace Avin.Infrastructure.Linq
{
    public class QueryInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly TakeInfo TakeInfo = new();

        /// <summary>
        /// 查询信息
        /// </summary>
        public readonly List<SelectInfo> SelectInfos = new();

        /// <summary>
        /// 关联表信息
        /// </summary>
        public readonly List<TableInfo> TableInfos = new();

        /// <summary>
        /// 查询条件
        /// </summary>
        public readonly StringBuilder Where = new();

        /// <summary>
        /// 分组信息
        /// </summary>
        public readonly List<GroupByInfo> GroupNewInfos = new();

        /// <summary>
        /// 分组信息
        /// </summary>
        public readonly List<GroupByInfo> GroupByInfos = new();

        /// <summary>
        /// 排序信息
        /// </summary>
        public readonly List<OrderByInfo> OrderByInfos = new();

        /// <summary>
        /// 查询参数
        /// </summary>
        public readonly List<ParamInfo> ParamInfos = new();

        /// <summary>
        /// where中是否进入!运算符
        /// </summary>
        public bool WhereIsInNot = false;

        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            TakeInfo.NeedDo = false;
            TakeInfo.Skip = 0;
            TakeInfo.Take = 10;
            SelectInfos.Clear();
            TableInfos.Clear();
            Where.Clear();
            GroupByInfos.Clear();
            OrderByInfos.Clear();
            ParamInfos.Clear();
            WhereIsInNot = false;
        }
    }

    public class TakeInfo
    {
        /// <summary>
        /// 需要执行Take
        /// </summary>
        public bool NeedDo { get; set; } = false;

        public uint Skip { get; set; } = 0;

        public uint Take { get; set; } = 10;
    }

    public class SelectInfo
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 字段别名
        /// </summary>
        public string OtherName { get; set; }
    }

    public class TableInfo
    {
        /// <summary>
        /// 哈希码，表示哪个表
        /// </summary>
        public int HashCode { get; set; }

        /// <summary>
        /// 表名称
        /// </summary>
        public string SourceName { get; set; }

        /// <summary>
        /// 表别名
        /// </summary>
        public string OtherName { get; set; }

        /// <summary>
        /// join方式
        /// </summary>
        public string JoinType { get; set; }

        /// <summary>
        /// join条件
        /// </summary>
        public StringBuilder Where { get; set; } = new();
    }

    public class GroupByInfo
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 字段别名
        /// </summary>
        public string OtherName { get; set; }
    }

    public class OrderByInfo
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 排序方式
        /// </summary>
        public string OrderBy { get; set; }
    }

    public class ParamInfo
    {
        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        public object Value { get; set; }

        /// <summary>
        /// 值的哈希码
        /// </summary>
        public int HashCode { get; set; }
    }
}
